package com.example.fangyh09.gridview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity implements AdapterView.OnItemClickListener{
    GridView gridView;
    List<Map<String,Object>> datalist;
    int []icon = {R.drawable.address_book,
            R.drawable.calendar,
            R.drawable.camera,
            R.drawable.clock,
            R.drawable.games_control,
            R.drawable.ic_launcher,
            R.drawable.messenger,
            R.drawable.ringtone,
            R.drawable.settings,
            R.drawable.speech_balloon,
            R.drawable.weather,
            R.drawable.world,
            R.drawable.youtube};
    String[] iconName = {"address_book",
            "calendar",
            "camera",
            "clock",
            "games_control",
            "ic_launcher",
            "messenger",
            "ringtone",
            "settings",
            "speech_balloon",
            "weather",
            "world",
            "youtube"};
    SimpleAdapter simpleAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gridView = (GridView)findViewById(R.id.gridview);
        datalist = new ArrayList<Map<String,Object>>();
        simpleAdapter = new SimpleAdapter(this,getData(),R.layout.item,new String[]{"image","text"},new int[] {R.id.pic,R.id.text});
        gridView.setAdapter(simpleAdapter);
        gridView.setOnItemClickListener(this);
    }

    private List<Map<String,Object>> getData() {
        for (int i = 0,sz = icon.length;i < sz;i ++) {
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("image",icon[i]);
            map.put("text",iconName[i]);
            datalist.add(map);
        }
        return datalist;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this,"I am" + iconName[position],Toast.LENGTH_LONG).show();
    }
}










